function uncheckTags(){
	
	var tagCheckboxes = document.getElementById('tagsCheckboxes');
	var boxes = tagCheckboxes.getElementsByTagName("div");
	var checkboxLength = boxes.length;
	
	for (var i = 0; i<checkboxLength; i++){
		var checkbox = document.getElementById("tag" + i);
		
		if (checkbox.checked) checkbox.checked=false;
	}
	
}

function browseFilterTags(){
	
	var tagCheckboxes = document.getElementById('tagsCheckboxes');
	var boxes = tagCheckboxes.getElementsByTagName("div");
	var checkboxLength = boxes.length;
	
	var tagindex = 0;
	var tagsURL = "";
	
	for (var i = 0; i<checkboxLength; i++){
		var checkbox = document.getElementById("tag" + i);
		
		if (checkbox.checked){
			tagsURL = tagsURL + "&tag" + tagindex + "=" + checkbox.value;
			tagindex++;
		}	
	}
	
	var sortMethod = sortVal;

	$.getJSON("/writeonproject/update?page=browse_stories_tags" + "&tagIndex=" + tagindex + tagsURL, function(data){
		
		if( data.error != null && data.error == "false" ) {
			
			var storybody = document.getElementById('storybody');
			
			for (var i = storybody.rows.length; i > 0; i--){
				storybody.deleteRow(0);
			}
			
			if( data.stories.length == 0 ){
				var bottomRow = storybody.insertRow ( 0 );
				bottomRow.className = "rowon";
				var bottom1 = bottomRow.insertCell( 0 );
				bottom1.className = "story_preview";
				bottom1.colSpan = "4";
				bottom1.innerHTML = "Sorry, there are no matching stories for the above criteria.";
			}
			
			if( data.stories.length > 0 ) {
				if (sortMethod == "updated"){
					data.stories.sort(sortByUpdated);			
				}
				
				else if (sortMethod == "viewed"){
					data.stories.sort(function(a, b){
						return a.viewcount - b.viewcount;});
				}
				
				else if (sortMethod == "voted"){
					data.stories.sort(function(a, b){
						return a.votecount - b.votecount;});		
				}
				
				else if (sortMethod == "created"){
					data.stories.sort(sortByCreatedTime);
				}
				
				else if (sortMethod == "commented"){
					data.stories.sort(sortByCommented);			
				}
				
				for( var i = 0; i < data.stories.length; i++ ) {
					
					var story = data.stories[i];
				
						var bottomRow = storybody.insertRow ( 0 );
						var topRow = storybody.insertRow( 0 );
						
						topRow.className = "rowon story1";
						var topcell1 = topRow.insertCell( 0 );
						var topcell2 = topRow.insertCell( 1 );
						var topcell3 = topRow.insertCell( 2 );
						var topcell4 = topRow.insertCell( 3 );
						
						topcell1.innerHTML = "<a href=\"/writeonproject/home?page=story_view&storyid=" + story.storyid + "\" class=\"title\">" + story.storytitle + "</a>";
						topcell2.className = "small_date";
						topcell2.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.authorid + "\" class=\"author\">" + story.authorid + "</a><br>" + story.createdate;
						topcell3.className = "story_post";
						topcell3.innerHTML = story.numpost;
						topcell4.className = "small_date";
						topcell4.id = "time" + story.storyid;
						topcell4.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp;
						
						bottomRow.className = "rowon";
						var bottom1 = bottomRow.insertCell( 0 );
						bottom1.className = "story_preview";
						bottom1.colSpan = "4";
						bottom1.innerHTML = story.preview;
					} 
			}
			}
		});
}

function updateTags() {
	var storyid = document.getElementById("storyid");
	$.getJSON("/writeonproject/update?page=story_view_tags&storyid=" + storyid.value, updateTagsCallBack);
}

function updateTagsCallBack(data) {
	if (data.error != null && data.error == "false") {
		var tags = document.getElementById("tag_container");
		
		if (tags != null && data.tags.length > 0) {
			for (var i = 0; i < data.tags.length; i++) {
				var tag = data.tags[i];
				var tag_box = document.getElementById("tagbox" + tag);
				alert(tag_box);
				if (tag_box != null) {
					var input = tag_box.getElementsByTagName("input");
					if (input != null && input[0] != null) {
						input[0].checked = true;
					}
				}
			}
		}		
	}
}

function loadTagContainer() {
	$.getJSON("/writeonproject/update?page=all_tags", loadTagContainerCallBack);
}

function loadTagContainerCallBack(data) {
	if (data.error != null && data.error == "false") {
		var tag_con = document.getElementById("tag_container");
		
		if (tag_con != null && data.tags.length > 0) {
			tag_con.innerHTML = "";
			
			for (var i = 0; i < data.tags.length; i++) {
				var tag = data.tags[i];

				if (i % 5 == 0) {
					tag_con.innerHTML += "<section class=\"tag_row\">";
				}
				
				tag_con.innerHTML += "<div class=\"tag_box\" id=\"tagbox" + tag + "\"><input type=\"checkbox\" value=\"" + tag + "\" id=\"tag" + i + "\">" + tag + "</div>";
				
				if ((i + 1) % 5 == 0) {
					tag_con.innerHTML += "</section>";
				}
			}
		}		
	}
}

function loadVerticalTagContainer() {
	$.getJSON("/writeonproject/update?page=all_tags", loadVerticalTagContainerCallBack);
}

function loadVerticalTagContainerCallBack(data) {
	if (data.error != null && data.error == "false") {
		var tag_con = document.getElementById("tagsCheckboxes");
		
		if (tag_con != null && data.tags.length > 0) {
			tag_con.innerHTML = "";
			
			for (var i = 0; i < data.tags.length; i++) {
				var tag = data.tags[i];
				tag_con.innerHTML += "<div class=\"filter_tag\"><input type=\"checkbox\" value=\"" + tag + "\" id=\"tag" + i + "\">" + tag + "</div>";
			}
		}		
	}
}